<?php

namespace App\Controller;

use Core\Log;

/**
 * Description of Pub
 *
 * @author lxc
 */
class Pub extends \Core\Controller
{

	public function __construct()
	{
		parent::__construct();
	}

	public function __call($name, $arguments)
	{
		parent::__call($name, $arguments);
	}

	/**
	 * 首页 - 列表展示
	 */
	public function index()
	{
		//当前页
		$page = abs(intval($this->_get('page')));
		if ($page < 1)
			$page = 1;
		$page_size = 10;
		$model = \Core\Db::getInstance();
		//获取总文章数
		$count = $model->count('select count(*) as _count_ from `bg_posts` where is_private=0 and is_show=1 and is_tmpe=0');
		if ($count == 0)
			return $this->_error('数据为空');
		$max_page = ceil($count / $page_size);
		if ($page > $max_page)
			$page = $max_page;
		$this->_assign('totalData', $count)->_assign('showData', $page_size)->_assign('current_page', $page);
		$list = $model->select('select * from `bg_posts` where is_private=0 and is_show=1 and is_tmpe=0 order by add_time desc limit ' . (($page - 1) * $page_size) . ',' . $page_size);
		$post_type_info = \App\Service\PostType::getAll();
		$this->_assign('list', $list)->_assign('post_type_info', $post_type_info);
		$this->_display('Pub/index');
	}

	/**
	 * 文章详情页
	 */
	public function read()
	{
		$id = (int) $this->_get('id');
		$model = \Core\Db::getInstance();
		//获取文章内容
		$cons = $model->find('select * from `bg_posts` where id=' . $id . ' limit 1');
		if (empty($cons))
			return $this->_error('获取文章失败');
		$this->_assign('cons', $cons);
		//获取文章评论列表
		$comment_list = $model->select('select * from `bg_reply` where contents_id=' . $id . ' and is_show = 1 order by reply_time desc limit 20');
		$this->_assign('comment_list', $comment_list);
		//获取文章的标签列表
		$tag_list = $model->select('SELECT `id`,`name` FROM `bg_posts_tags` WHERE `contents_id`=' . $id . ' ORDER BY `add_time` ');
		$this->_assign('tag_list', $tag_list);
		//获取所有分类信息
		$post_type_info = \App\Service\PostType::getAll();
		$this->_assign('post_type_info', $post_type_info);
		//阅读数+1
		$model->update('update `bg_posts` set read_num=read_num+1 where id=' . $id . ' limit 1');
		$this->_display();
	}

	/**
	 * 搜索列表页
	 */
	public function lists()
	{
		$model = \Core\Db::getInstance();
		$sql_where = [
			'is_private=0',
			'is_show=1',
			'is_tmpe=0'
		];
		//分类
		$type = $this->_get('postType');
		Log::write("\$type:$type ");
		//获取所有分类信息
		$post_type_info = \App\Service\PostType::getAll();
		if ($type && isset($post_type_info[$type])) {
			$type_info = $post_type_info[$type];
			if ($type_info['fid'] == 0) {
				$ids = $model->select('select id from `bg_posts_type` where fid=' . $type);
				if (empty($ids)) {
					$sql_where[] = 'post_type=' . $type;
				} else {
					$type_id_arr = [$type];
					foreach ($ids as $v)
						$type_id_arr[] = $v['id'];
					$sql_where[] = 'post_type in (' . implode(',', $type_id_arr) . ')';
				}
			} else {
				$sql_where[] = 'post_type=' . $type;
			}
		}
		$this->_assign('post_type', $type);

		//标签
		$tag = $this->_get('tag');
		if ($tag != '') {
			$cons_data = $model->select('select `contents_id` from `bg_posts_tags` where `name`="' . $tag . '" group by `contents_id`');
			if (!empty($cons_data)) {
				$id_arr = [];
				foreach ($cons_data as $v)
					$id_arr[] = $v['contents_id'];
				$sql_where[] = 'id in(' . implode(',', $id_arr) . ')';
			}
		}
		$this->_assign('post_tag', $tag);

		//key
		$key = $this->_get('key');
		if ($key != '') {
			$sql_where[] = 'post_title like "%' . $key . '%"';
		}
		$this->_assign('post_key', $key);

		//page
		$page = abs(intval($this->_get('page')));
		$page_size = 20;
		$sql_count = 'select count(*) as _count_ from `bg_posts` where ' . implode(' and ', $sql_where);

		$count = $model->count($sql_count);
		if ($count == 0)
			return $this->_error('数据为空');
		$max_page = ceil($count / $page_size);
		//page
		if ($page < 1)
			$page = 1;
		if ($page > $max_page)
			$page = $max_page;
		$this->_assign('totalData', $count)->_assign('showData', $page_size)->_assign('current_page', $page);
		$cons_lists = $model->select($sql = 'select * from `bg_posts` where ' . implode(' and ', $sql_where) . ' order by id desc limit ' . (($page - 1) * $page_size) . ',' . $page_size);
		$this->_assign('cons_lists', $cons_lists);
		$this->_assign('page', $page)->_assign('page_size', $page_size)->_assign('post_type_info', $post_type_info);
		$this->_display();
	}

	/**
	 * 添加评论
	 */
	public function reply_add()
	{
		$content_id = $this->_get('content_id');
		$user_name = $this->_post('user_name');
		$user_eamil = $this->_post('user_email');
		$user_website = $this->_post('user_website');
		$reply_contents = $this->_post('reply_contents');

		if ($reply_contents == '')
			return $this->_error('评论内容为空');

		$model = \Core\Db::getInstance();
		$contents = $model->find('select id,is_remark,is_private,is_show,is_tmpe from `bg_posts` where id=' . $content_id);
		if (empty($contents))
			return $this->_error('未找到该文章');
		if ($contents['is_remark'] != 1 || $contents['is_private'] == 1 || $contents['is_show'] != 1 || $contents['is_tmpe'] == 1)
			return $this->_error('该文章不允许评论');

		$res = $model->add($sql = 'insert into bg_reply (contents_id,user_name,user_email,user_website,is_show,reply_contents,reply_time) values '
			. '(' . $content_id . ',"' . addslashes($user_name) . '","' . addslashes($user_eamil) . '","' . addslashes($user_website) . '",1,"' . addslashes($reply_contents) . '","' . time() . '"' . ')');
		echo "\r\n$sql\r\n";
		if (!$res)
			return $this->_error('评论失败');
		return $this->_success('评论成功', 0, '/Pub/read/id/' . $content_id . '.html');
	}

	/**
	 * RSS订阅
	 */
	public function showrss()
	{
		$this->_display('rss');
	}

	/**
	 * 需求建议
	 */
	public function suggest()
	{
		$this->_display();
	}

	/**
	 * 添加友链
	 */
	public function addLink()
	{
		$this->_display('addlink');
	}

	/**
	 * 关于我
	 */
	public function about()
	{
		$this->_display();
	}

}
